﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Com.CMS263.BLL;
using Com.CMS263.PO;
using Com.CMS263.Util;


namespace Com.CMS263.Web.manage
{
    public partial class edit_lxwm : System.Web.UI.Page
    {
        AboutHelpBLL ahbll = new AboutHelpBLL();
        public string mail_server = "";
        public string mail_from = "";
        public string mail_frompass = "";
        public int count=0;
        EmailSendBLL esbll = new EmailSendBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }
        public void BindData()
        {
            AboutHelp ah = new AboutHelp();
            ah = ahbll.GetAboutHelpById(4);

            AppSettingsReader reader = new AppSettingsReader();
            mail_server = ah.title; //reader.GetValue("MailServer", typeof(string)).ToString();
            mail_from = ah.contents;//reader.GetValue("MailServerUser", typeof(string)).ToString();
            mail_frompass = ah.remark;//reader.GetValue("MailServerPassword", typeof(string)).ToString();
            TxtSmtp.Text = mail_server;
            txtFrom.Text = mail_from;
            txtFromPass.Text = mail_frompass;
        }

        public string msg = "";
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string title = txtTitle.Text;
            string contents = Hidden1.Value;
            if (string.IsNullOrEmpty(title))
            {
                StringUtil.Alert("请输入发送标题", "-1");
            }
            else if (string.IsNullOrEmpty(contents))
            {
                StringUtil.Alert("请输入发送标题", "-1");
            }
            else
            {
                txtTitle.Text = "";
                Hidden1.Value = "";
                IList<EmailSend> eslist = new List<EmailSend>();
                eslist = esbll.GetAllEmailSend(rbType.SelectedValue);
                for (int i = 0; i < eslist.Count; i++)
                {
                    try
                    {
                        
                        StringUtil.SendSMTPEMail(mail_server, mail_from, mail_frompass, eslist[i].email, title, contents);
                        count++;
                    }
                    catch 
                    {
                        StringUtil.Alert("发送邮箱有问题,请核实","-1");
                    }
                    
                }

                StringUtil.Alert("发送完毕,成功发送"+count+"条", "-1");
            }


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string server = TxtSmtp.Text;
            string fromuser = txtFrom.Text;
            string userpass = txtFromPass.Text;
            if (string.IsNullOrEmpty(server))
            {
                StringUtil.Alert("发送服务器(SMTP)不能为空", "-1");
            }
            if (string.IsNullOrEmpty(fromuser))
            {
                StringUtil.Alert("发送邮箱", "-1");
            }
            if (!StringUtil.isEmail(fromuser))
            {
                StringUtil.Alert("发送邮箱格式错误", "-1");
            }
            if (string.IsNullOrEmpty(userpass))
            {
                StringUtil.Alert("发送邮箱密码", "-1");
            }
            AboutHelp ah = new AboutHelp();
            ah = ahbll.GetAboutHelpById(4);
            ah.remark = userpass;
            ah.contents = fromuser;
            ah.title = server;
            ahbll.ModifyAboutHelp(ah);
            //ConfigurationManager.AppSettings["MailServer"] = TxtSmtp.Text;
            //ConfigurationManager.AppSettings["MailServerUser"] = txtFrom.Text;
            //ConfigurationManager.AppSettings["MailServerPassword"] =txtFromPass.Text;
            StringUtil.Alert("修改成功", "edit_lxwm.aspx");
        }
    }
}
